package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.ContractDAO;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.CustomerShareDAO;
import com.feeyin.radar.crm.dao.SaleRecordDAO;
import com.feeyin.radar.crm.model.Contract;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.SaleRecord;
import com.feeyin.radar.crm.service.RepeatCustomerService;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.ManageSetDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.ManageSet;
import com.feeyin.radar.sys.model.ManageSetObject;
import com.feeyin.radar.sys.model.Users;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RepeatCustomerServiceImpl
  implements RepeatCustomerService
{
  private CustomerDAO customerDAO;
  private UserDAO userDAO;
  private DepartmentDAO departmentDAO;
  private ManageSetDAO manageSetDAO;
  private CustomerShareDAO customerShareDAO;
  private SaleRecordDAO saleRecordDAO;
  private ContractDAO contractDAO;

  public CustomerDAO getCustomerDAO()
  {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public ManageSetDAO getManageSetDAO() {
    return this.manageSetDAO;
  }

  public void setManageSetDAO(ManageSetDAO manageSetDAO) {
    this.manageSetDAO = manageSetDAO;
  }

  public CustomerShareDAO getCustomerShareDAO() {
    return this.customerShareDAO;
  }

  public void setCustomerShareDAO(CustomerShareDAO customerShareDAO) {
    this.customerShareDAO = customerShareDAO;
  }

  public SaleRecordDAO getSaleRecordDAO() {
    return this.saleRecordDAO;
  }

  public void setSaleRecordDAO(SaleRecordDAO saleRecordDAO) {
    this.saleRecordDAO = saleRecordDAO;
  }

  public ContractDAO getContractDAO() {
    return this.contractDAO;
  }

  public void setContractDAO(ContractDAO contractDAO) {
    this.contractDAO = contractDAO;
  }

  public List getPageRepeatCustomers(Integer currentUserId, String customerName, Page page) {
    if (currentUserId == null) return null;

    Users user = new Users();
    user = this.userDAO.getUser(currentUserId);
    if (user == null) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    Customer customer = new Customer();
    if (!StringUtils.isEmpty(customerName)) {
      customer.setSearchCustomerName(customerName);
    }
    if (StringUtils.isEmpty(deptIds)) return null;
    String categorys = "1,2,3,5,6,7";
    if (this.manageSetDAO.isUseCommons() == 2) {
      categorys = "0,1,2,3,5,6,7";
    }
    customer.setCategorys(categorys);
    customer.setDeptIds(deptIds);

    List repeatSets = new ArrayList();
    repeatSets = this.manageSetDAO.getRepeatSets();
    if (repeatSets == null)
      customer.setCustomerName("name");
    else {
      for (int i = 0; i < repeatSets.size(); i++) {
        ManageSetObject manageSetObject = new ManageSetObject();
        manageSetObject = (ManageSetObject)repeatSets.get(i);
        if (manageSetObject.getOtherId().intValue() == 2)
          customer.setCustomerName("name");
        else if (manageSetObject.getOtherId().intValue() == 3)
          customer.setAddress("address");
        else if (manageSetObject.getOtherId().intValue() == 14)
          customer.setHttp("http");
        else if (manageSetObject.getOtherId().intValue() == 4)
          customer.setPhone("phone");
        else if (manageSetObject.getOtherId().intValue() == 17)
          customer.setCustomerSimple("simple");
        else if (manageSetObject.getOtherId().intValue() == 5) {
          customer.setFax("fax");
        }
      }
    }

    List customers = new ArrayList();
    customers = this.customerDAO.getPageRepeatCustomersByDynamic(customer, page);
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);
        user = new Users();
        user = this.userDAO.getUser(cCustomer.getSalePersonId());
        if (user != null) {
          cCustomer.setSalePerson(user.getUserName());
        }

        customer.setLastLink(this.saleRecordDAO.getSaleRecordLastLinkByCustomerId(customer.getId()));
      }
    }
    return customers;
  }

  public Integer getPageRepeatCustomersCount(Integer currentUserId, String customerName) {
    if (currentUserId == null) return Integer.valueOf(0);

    Users user = new Users();
    user = this.userDAO.getUser(currentUserId);
    if (user == null) return Integer.valueOf(0);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    Customer customer = new Customer();
    if (!StringUtils.isEmpty(customerName)) {
      customer.setSearchCustomerName(customerName);
    }
    if (StringUtils.isEmpty(deptIds)) return Integer.valueOf(0);
    String categorys = "1,2,3,5,6,7";
    if (this.manageSetDAO.isUseCommons() == 2) {
      categorys = "0,1,2,3,5,6,7";
    }
    customer.setCategorys(categorys);
    customer.setDeptIds(deptIds);

    List repeatSets = new ArrayList();
    repeatSets = this.manageSetDAO.getRepeatSets();
    if (repeatSets == null)
      customer.setCustomerName("name");
    else {
      for (int i = 0; i < repeatSets.size(); i++) {
        ManageSetObject manageSetObject = new ManageSetObject();
        manageSetObject = (ManageSetObject)repeatSets.get(i);
        if (manageSetObject.getOtherId().intValue() == 2)
          customer.setCustomerName("name");
        else if (manageSetObject.getOtherId().intValue() == 3)
          customer.setAddress("address");
        else if (manageSetObject.getOtherId().intValue() == 14)
          customer.setHttp("http");
        else if (manageSetObject.getOtherId().intValue() == 4)
          customer.setPhone("phone");
        else if (manageSetObject.getOtherId().intValue() == 17)
          customer.setCustomerSimple("simple");
        else if (manageSetObject.getOtherId().intValue() == 5) {
          customer.setFax("fax");
        }
      }
    }

    return this.customerDAO.getPageRepeatCustomersCountByDynamic(customer);
  }

  public List getRepeatCustomerInfos(Integer currentUserId, Integer customerId)
  {
    if (currentUserId == null) return null;
    if (customerId == null) return null;

    Customer selectCustomer = new Customer();
    selectCustomer = this.customerDAO.getCustomerById(customerId);
    if (selectCustomer == null) return null;

    Users user = new Users();
    user = this.userDAO.getUser(currentUserId);
    if (user == null) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    Customer customer = new Customer();
    if (StringUtils.isEmpty(deptIds)) return null;
    String categorys = "1,2,3,5,6,7";
    if (this.manageSetDAO.isUseCommons() == 2) {
      categorys = "0,1,2,3,5,6,7";
    }
    customer.setCategorys(categorys);
    customer.setDeptIds(deptIds);

    List repeatSets = new ArrayList();
    repeatSets = this.manageSetDAO.getRepeatSets();
    if (repeatSets == null)
      customer.setCustomerName(selectCustomer.getCustomerName());
    else {
      for (int i = 0; i < repeatSets.size(); i++) {
        ManageSetObject manageSetObject = new ManageSetObject();
        manageSetObject = (ManageSetObject)repeatSets.get(i);
        if (manageSetObject.getOtherId().intValue() == 2)
          customer.setCustomerName(selectCustomer.getCustomerName());
        else if (manageSetObject.getOtherId().intValue() == 3)
          customer.setAddress(selectCustomer.getAddress());
        else if (manageSetObject.getOtherId().intValue() == 14)
          customer.setHttp(selectCustomer.getHttp());
        else if (manageSetObject.getOtherId().intValue() == 4)
          customer.setPhone(selectCustomer.getPhone());
        else if (manageSetObject.getOtherId().intValue() == 17)
          customer.setCustomerSimple(selectCustomer.getCustomerSimple());
        else if (manageSetObject.getOtherId().intValue() == 5) {
          customer.setFax(selectCustomer.getFax());
        }
      }
    }

    List customers = new ArrayList();
    customers = this.customerDAO.getCustomersByDynamic(customer);
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);
        user = new Users();
        user = this.userDAO.getUser(cCustomer.getSalePersonId());
        if (user != null) {
          cCustomer.setSalePerson(user.getUserName());
        }

        customer.setLastLink(this.saleRecordDAO.getSaleRecordLastLinkByCustomerId(customer.getId()));
      }
    }
    return customers;
  }

  public boolean mergeCustomer(Integer currentUserId, Integer selectCustomerId, Integer mainCustomerId, Integer salePersonId)
  {
    if (currentUserId == null) return false;
    if (selectCustomerId == null) return false;
    if (mainCustomerId == null) return false;

    List repeatCustomers = new ArrayList();
    repeatCustomers = getRepeatCustomerInfos(currentUserId, selectCustomerId);

    if (repeatCustomers == null) return false;
    Customer mainCustomer = new Customer();
    mainCustomer = this.customerDAO.getCustomerById(mainCustomerId);
    for (int i = 0; i < repeatCustomers.size(); i++) {
      Customer customer = new Customer();
      customer = (Customer)repeatCustomers.get(i);
      if (mainCustomerId.compareTo(customer.getId()) != 0) {
        Customer paramCustomer = new Customer();
        paramCustomer.setId(customer.getId());
        paramCustomer.setCustomerCategory("9");
        boolean result = false;
        result = this.customerDAO.updateCustomer(paramCustomer);

        this.customerShareDAO.deleteCustomerShare(customer.getId());

        if (mainCustomer != null) {
          insertSaleRecordToMainCustomer(mainCustomerId, customer.getId());
          insertContractToMainCustomer(mainCustomerId, customer.getId());
        }
      }
    }

    if ((mainCustomer != null) && (salePersonId != null)) {
      Customer paramCustomer = new Customer();
      paramCustomer.setId(mainCustomer.getId());
      paramCustomer.setCustomerCategory("1");
      paramCustomer.setSalePersonId(salePersonId);
      paramCustomer.setUpdateCustomerDate(new Date());
      paramCustomer.setUpdateSaleRecordDate(new Date());
      paramCustomer.setUpdateSaleStageDate(new Date());
      boolean result = false;
      result = this.customerDAO.updateCustomer(paramCustomer);
      if (result)
      {
        this.customerDAO.updateCustomerShare(mainCustomer.getId(), "0");
        this.customerShareDAO.deleteCustomerShare(mainCustomer.getId());

        this.customerShareDAO.insertCustomerShare(mainCustomer.getId(), salePersonId, "1");
      }
    }

    return true;
  }

  public boolean isRepeatSet() {
    List manageSetObjects = new ArrayList();

    ManageSet mSet = new ManageSet();
    ManageSet manageSet = new ManageSet();
    manageSet.setMType("5");
    mSet = this.manageSetDAO.getManageSetByTypeStatus(manageSet);

    return (mSet == null) || (!"1".equals(mSet.getStatus()));
  }

  private boolean insertSaleRecordToMainCustomer(Integer mainCustomerId, Integer customerId)
  {
    if (mainCustomerId == null) return false;
    if (customerId == null) return false;
    List saleRecords = new ArrayList();
    saleRecords = this.saleRecordDAO.getSaleRecordsByCustomerId(customerId);
    if ((saleRecords == null) || (saleRecords.size() <= 0)) return false;
    for (int i = 0; i < saleRecords.size(); i++) {
      SaleRecord saleRecord = new SaleRecord();
      saleRecord = (SaleRecord)saleRecords.get(i);
      this.saleRecordDAO.updateSaleRecordCustomerIdById(mainCustomerId, saleRecord.getId());
    }
    return true;
  }

  private boolean insertContractToMainCustomer(Integer mainCustomerId, Integer customerId) {
    if (mainCustomerId == null) return false;
    if (customerId == null) return false;
    List contracts = new ArrayList();
    contracts = this.contractDAO.getContractsByCustomerId(customerId);
    if ((contracts == null) || (contracts.size() <= 0)) return false;
    for (int i = 0; i < contracts.size(); i++) {
      Contract contract = new Contract();
      contract = (Contract)contracts.get(i);
      this.contractDAO.updateContractCustomerIdById(mainCustomerId, contract.getId());
    }
    return true;
  }
}